unit Unit11; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Menus, Unit1, Unit12, Unit13, Unit14, LR_DBSet, LR_Class, Unit59;

type

  { TForm11 }

  TForm11 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem12Click(Sender: TObject);
    procedure MenuItem14Click(Sender: TObject);
    procedure MenuItem15Click(Sender: TObject);
    procedure ordenar_grid();
    procedure MenuItem11Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
  private
    nombre : String;
    flia_productos : String;
    fabricante : String;
  public
    { public declarations }
  end; 

var
  Form11: TForm11; 

implementation

{ TForm11 }

procedure TForm11.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.SQL.Text := 'select * from vw_producto where prd_nombre ilike ''%' + nombre
      + '%'' and fmp_nombre like ''' + flia_productos + ''' and fab_nombre like ''%'
      + fabricante + '%'' order by prd_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Descripción';
  DBGrid1.Columns[3].Title.Caption := 'Familia de Productos';
  DBGrid1.Columns[4].Title.Caption := 'Fabricante';
  DBGrid1.Columns[5].Title.Caption := 'Precio 1';
  DBGrid1.Columns[6].Title.Caption := 'Precio 2';
  DBGrid1.Columns[7].Title.Caption := 'Precio 3';
  DBGrid1.Columns[8].Title.Caption := 'Precio 4';
  DBGrid1.Columns[9].Title.Caption := 'Stk. Min.';
  DBGrid1.Columns[10].Title.Caption := 'Stk. Max.';

  DBGrid1.Columns[0].Width := 60;
  DBGrid1.Columns[1].Width := 200;
  DBGrid1.Columns[2].Width := 195;
  DBGrid1.Columns[3].Width := 125;
  DBGrid1.Columns[4].Width := 125;
  DBGrid1.Columns[5].Width := 80;
  DBGrid1.Columns[6].Width := 80;
  DBGrid1.Columns[7].Width := 80;
  DBGrid1.Columns[8].Width := 80;
  DBGrid1.Columns[9].Width := 80;
  DBGrid1.Columns[10].Width := 80;

  TColumn(DBGrid1.Columns[5]).DisplayFormat := '$ ####0.00';
  TColumn(DBGrid1.Columns[6]).DisplayFormat := '$ ####0.00';
  TColumn(DBGrid1.Columns[7]).DisplayFormat := '$ ####0.00';
  TColumn(DBGrid1.Columns[8]).DisplayFormat := '$ ####0.00';

  DBGrid1.Columns[6].Visible := False;
  DBGrid1.Columns[7].Visible := False;
  DBGrid1.Columns[8].Visible := False;
  {DBGrid1.Columns[9].Visible := False;
  DBGrid1.Columns[10].Visible := False;}
  DBGrid1.Columns[11].Visible := False;
  DBGrid1.Columns[12].Visible := False;
end;

procedure TForm11.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  nombre := '%';
  flia_productos := '%';
  fabricante := '%';
  
  ordenar_grid;
end;

procedure TForm11.MenuItem10Click(Sender: TObject);
begin
  Form59.ShowModal;
  fabricante := Form59.nombre_fabricante;
  ordenar_grid();
end;

procedure TForm11.MenuItem12Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_productos.lrf');
  frReport1.ShowReport;
end;

procedure TForm11.MenuItem14Click(Sender: TObject);
var
  nombre_fabricante : string;
  porcentaje : string;
  QueryString : string;

begin
  Form59.ShowModal;
  nombre_fabricante := Form59.nombre_fabricante;

  porcentaje := InputBox('Modificación porcentual de precio',
    'Ingrese porcentaje a modificar (+ aumenta / - disminuye):', '');

  QueryString := 'select modificar_precio_fabricante(' + porcentaje + ', '''
    + nombre_fabricante + ''')';

  Form1.PQConnection1.ExecuteDirect('BEGIN WORK;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('COMMIT WORK;');

  ordenar_grid();
end;

procedure TForm11.MenuItem15Click(Sender: TObject);
var
  familia_productos : string;
  porcentaje : string;
  QueryString : string;

begin
  Form14.ShowModal;
  familia_productos := Form14.flia_productos;

  porcentaje := InputBox('Modificación porcentual de precio',
    'Ingrese porcentaje a modificar (+ aumenta / - disminuye):', '');

  QueryString := 'select modificar_precio_fmp(' + porcentaje + ', '''
    + familia_productos + ''')';

  Form1.PQConnection1.ExecuteDirect('BEGIN WORK;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('COMMIT WORK;');

  ordenar_grid();
end;

procedure TForm11.MenuItem11Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm11.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm11.DBGrid1CellClick(Column: TColumn);
begin
  Form13.codigo := SQLQuery1.FieldByName('prd_cdg').AsString;
  Form13.ShowModal;
  ordenar_grid;
end;

procedure TForm11.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm11.MenuItem4Click(Sender: TObject);
begin
   Form12.ShowModal;
   FormShow;
end;

procedure TForm11.MenuItem5Click(Sender: TObject);
var
  codigo : String;
  QueryString : String;

begin
  codigo := InputBox('Baja de Producto', 'Ingrese el código del producto a eliminar:', '');

  if codigo <> '' then
  begin
    QueryString := 'delete from producto where prd_cdg = ' + codigo;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('BAJA EXITOSA');
  end;

  FormShow;
end;

procedure TForm11.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en el producto a modificar');
end;

procedure TForm11.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtrado de Productos por Nombre', 'Ingrese parte del nombre del producto:', '');
  
  if nombre <> '' then
    ordenar_grid;
end;

procedure TForm11.MenuItem9Click(Sender: TObject);
begin
  Form14.ShowModal;
  flia_productos := Form14.flia_productos;
  
  if flia_productos <> '' then
    ordenar_grid;
end;

initialization
  {$I unit11.lrs}

end.

